/*
 * @Author: 李一番 
 * @Date: 2021-12-17 17:30:17 
 * @Last Modified by:   李一番 
 * @Last Modified time: 2021-12-17 17:30:17 
 */
import { ActionContext } from 'vuex'
import { ITagsViewState, TagView } from './state'
import { IStore } from '@/store/types'

// 操作流
export const actions = {
  addView({ commit }: ActionContext<ITagsViewState, IStore>, view: TagView) {
    commit('ADD_VISITED_VIEW', view)
    commit('ADD_CACHED_VIEW', view)
  },
  addVisitedView({ commit }: ActionContext<ITagsViewState, IStore>, view: TagView) {
    commit('ADD_VISITED_VIEW', view)
  },
  addCachedView({ commit }: ActionContext<ITagsViewState, IStore>, view: TagView) {
    commit('ADD_CACHED_VIEW', view)
  },
  delView({ dispatch, state }: ActionContext<ITagsViewState, IStore>, view: TagView) {
    return new Promise(resolve => {
      dispatch('delVisitedView', view)
      dispatch('delCachedView', view)
      resolve({
        visitedViews: [...state.visitedViews],
        cachedViews: [...state.cachedViews]
      })
    })
  },
   delVisitedView({ commit, state }: ActionContext<ITagsViewState, IStore>, view: TagView) {
    return new Promise(resolve => {
      commit('DEL_VISITED_VIEW', view)
      resolve([...state.visitedViews])
    })
  },
  delCachedView({ commit, state }: ActionContext<ITagsViewState, IStore>, view: TagView) {
    return new Promise(resolve => {
      commit('DEL_CACHED_VIEW', view)
      resolve([...state.cachedViews])
    })
  },

  delOthersViews({ dispatch, state }: ActionContext<ITagsViewState, IStore>, view: TagView) {
    return new Promise(resolve => {
      dispatch('delOthersVisitedViews', view)
      dispatch('delOthersCachedViews', view)
      resolve({
        visitedViews: [...state.visitedViews],
        cachedViews: [...state.cachedViews]
      })
    })
  },
  delOthersVisitedViews({ commit, state }: ActionContext<ITagsViewState, IStore>, view: TagView) {
    return new Promise(resolve => {
      commit('DEL_OTHERS_VISITED_VIEWS', view)
      resolve([...state.visitedViews])
    })
  },
  delOthersCachedViews({ commit, state }: ActionContext<ITagsViewState, IStore>, view: TagView) {
    return new Promise(resolve => {
      commit('DEL_OTHERS_CACHED_VIEWS', view)
      resolve([...state.cachedViews])
    })
  },

  delAllViews({ dispatch, state }: ActionContext<ITagsViewState, IStore>, view: TagView) {
    return new Promise(resolve => {
      dispatch('delAllVisitedViews', view)
      dispatch('delAllCachedViews', view)
      resolve({
        visitedViews: [...state.visitedViews],
        cachedViews: [...state.cachedViews]
      })
    })
  },
  delAllVisitedViews({ commit, state }: ActionContext<ITagsViewState, IStore>) {
    return new Promise(resolve => {
      commit('DEL_ALL_VISITED_VIEWS')
      resolve([...state.visitedViews])
    })
  },
  delAllCachedViews({ commit, state }: ActionContext<ITagsViewState, IStore>) {
    return new Promise(resolve => {
      commit('DEL_ALL_CACHED_VIEWS')
      resolve([...state.cachedViews])
    })
  },

  updateVisitedView({ commit }: ActionContext<ITagsViewState, IStore>, view: TagView) {
    commit('UPDATE_VISITED_VIEW', view)
  }
}
